import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';

import 'global.dart';

class ImageUtil {
  ImageUtil._();

  static ImageIcon icon(String path, {double? size, Color? color}) => ImageIcon(AssetImage(path), size: size ?? Global.ds(20), color: color);

  static Image asset(String path, {double? width, double? height, double? size, BoxFit fit = BoxFit.cover}) => Image.asset(
    path,
    width: width ?? size,
    height: height ?? size,
    isAntiAlias: true,
    filterQuality: FilterQuality.high,
    fit: fit,
  );

  static Image memory(Uint8List bytes, {double? width, double? height, double? size, BoxFit fit = BoxFit.cover}) => Image.memory(
    bytes,
    width: width ?? size,
    height: height ?? size,
    isAntiAlias: true,
    filterQuality: FilterQuality.high,
    fit: fit,
  );

  static CachedNetworkImage network(String url, {double? width, double? height, double? size, BoxFit fit = BoxFit.cover}) => CachedNetworkImage(
    imageUrl: url,
    width: width ?? size,
    height: height ?? size,
    filterQuality: FilterQuality.high,
    fit: fit,
    placeholder: (context, url) => CircularProgressIndicator(color: ColorUtil.color("#C0C4CC")),
    errorWidget: (context, url, error) => Icon(
      Icons.error,
      color: ColorUtil.color("#C0C4CC"),
      size: max(min<double>(width ?? 0, height ?? 0), Global.ds(10)),
    ),
  );

  static Widget avatar(String? url, double size) => (url?.isEmpty ?? true) ? asset("static/images/default-avatar.png", size: size) : network(url!, size: size);
}
